#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define BUFF 1024

char * keys,*str;

void mixed(char *,char*,int,int);
void swap(char *a,char *b);
void sort(char *);

int main(int argc, char ** argv)
{
	int key_sz,i,ch=0,sk=0;	
	char in='a',kl=0;;

	keys=malloc(26);
	str=(char*) malloc(BUFF);

	printf("\nEnter the key(max 26):");
	scanf("%s",keys);
	printf("\nGenerating keymap.");
	
	keys[25]=0;
	shrink(keys);
	key_sz=strlen(keys);
	//printf("\nKeys lenght is %d\n",key_sz);
	sort(keys);
	//printf("\nSorted key is:%s",keys);
	for(i=strlen(keys);in<='z';in++)
	{
		if(!strchr(keys,(int)in)) {keys[i]=in;i++;keys[i]=0;}
	}
	printf("\nKeymap generated:%s\n\n",keys);
	
	while(1)
	{
		printf("\n--------Program menu--------");
		printf("\n1.Encrypt the data");
		printf("\n2.Decrypt the data");
		printf("\nEnter 0 to exit the program");
		printf("\nEnter yout choice:");
		scanf("%d",&ch);
		
		if(!ch) break;	
		printf("\nEnter the text:");
		getchar();
		scanf("%[^\n]",str);
		printf("\nU entered %s\n",str);
		printf("\nEnter the key letter:");
		scanf("%c",&kl);	
		mixed(str,keys,ch,key_sz);	
		printf("\nResult is :%s\n",str);
	}
}
void mixed(char *str,char *keys,int ch,int key_sz)
{
	int len=strlen(str);
	int rows=len/key_sz;
	if(len%key_sz) rows++;
	char mat[rows][cols];
	
	int i=0,j=0;
	for(i=0;i<rows;i++)
	for(j=0;j<cols;j++)
	mat[i]
	
}
int shrink(char *str)
{
	int i=0,j=0;
	char c_char;
	int len=strlen(str);
	while(i<len)
	{
		for(j=i+1;j<len;j++) 
		{
			
			while((str[len-1]==str[i])&&(len>i+1)) 
			{
				len--;
				str[len]=0;
				//printf("\nString shrunk. Current string is :%s",str);
			}
			if(str[j]==str[i]) 
			{ 
				//printf("\nSwapping %c with %c",str[j],str[len-1]);
				swap(&str[j],&str[len-1]); 
				len--;
				str[len]=0;
				//printf("\nCurrent string is:%s",str);
			}
		}
		i++;
	}
		
}
void sort(char * arr)
{
	int len=strlen(arr);
	int j,i;
	for(i=0;i<len;i++)
	for(j=i;j<len;j++)
	if(arr[i]>arr[j]) swap(&arr[i],&arr[j]);
}
void swap(char *a,char *b)
{
	char c;
	c=*a;
	*a=*b;
	*b=c;
}
